import { BadRequestException, Body, Controller, Get, Param, Post } from '@nestjs/common';
import { FriendshipService } from './friendship.service';
import { RequireLogin, UserInfo } from "src/custom.decorator";
import { FriendAddDto } from "./dto/friend-add.dto";

@Controller('friendship')
export class FriendshipController {
  constructor(private readonly friendshipService: FriendshipService) { }

  /* 发送好友申请 */
  @Post('add')
  @RequireLogin()
  async add(@Body() friendAddDto: FriendAddDto, @UserInfo('userId') userId: number) {
    return this.friendshipService.add(friendAddDto, userId);
  }

  /* 获取当前用户的好友申请列表 */
  @Get('request_list')
  @RequireLogin()
  async list(@UserInfo('userId') userId: number) {
    return this.friendshipService.list(userId);
  }

  /* 同意&拒绝请求 */
  @Get('agree/:id')
  @RequireLogin()
  async agree(@Param('id') friendId: number, @UserInfo('userId') userId: number) {
    if (!friendId) throw new BadRequestException('同意好友申请的id不能为空');

    return this.friendshipService.agree(friendId, userId);
  }

  @Get('reject/:id')
  @RequireLogin()
  async reject(@Param('id') friendId: number, @UserInfo("userId") userId: number) {
    if (!friendId) throw new BadRequestException('拒绝好友申请的id不能为空');

    return this.friendshipService.reject(friendId, userId);
  }

  /* 删除好友关系 */
  @Get('remove/:id')
  async remove(@Param('id') friendId: number, @UserInfo('userId') userId: number) {
    return this.friendshipService.remove(friendId, userId);
  }
}
